package com.tea.room.util;

import java.io.StringReader;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.tea.room.model.xmlBean.Xml2Map;
import com.tea.room.util.wechat.encodingAESKey.WXBizMsgCrypt;

public class AesDemo {
    public static void main(String[] args) throws Exception {
        String encodingAesKey = "NkwaBoiQ8Hri5bQSOo0mo29qmnSt55UdzBUXfYb74v3";
        String token = "tea";
        String timestamp = "1554883459";
        String nonce = "1741383209";
        String appId = "wx1db2ce96ffcde003";
        String mingwen = "<xml><ToUserName><![CDATA[gh_1046f29b0c76]]></ToUserName><Encrypt><![CDATA[Jk7lapGyWNZA0bjN8QmBlu4uiSkqCs0Z31cMsVhFsAUnKVecRhnPaGkPc8Nb8g7nDef7XA6TVKmPRZNNxXkE+jsUqyteIEDsURtU7aOkwCLFSZ1Yt3OLqzW0IilEAxSQd0AZNDdPjnKEoM+53Zit/jDOXDBKQn9ad0r42uK9jwEPZl1W0PW0kElf0y4MT2D99xRwICBKMmeq7i/PM+r7FxYQKDEyYQgXAV0Y8FZ30iPIHxDi+wlW8YMEg5fGkxigVMb40QJzFtEd0WDrUkw8bSCTif12UTPJvqQVw04rrP/hIzQz7ZP1GnfQ5ItFMSVgK9mIn1itPf4VfhnRs82d+zxpX70P4aBYmas0kmFJFR1oS414/0VdZumas2WusTiMFSbVUBgZ7jGOckiQtdK3kB9WSWKVnlkHAL3rHO8xEMI=]]></Encrypt></xml>";
        String msgSignature = "e41ae2b5bf3a39729beffef7949b0f684fd88b2a";
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        //
        // 公众平台发送消息给第三方，第三方处理
        //
        // 第三方收到公众号平台发送的消息
        String xml = pc.decryptMsg(msgSignature, timestamp, nonce, mingwen);
        System.out.println("解密后明文: " + xml);
        
      //回复公众平台，加密返回
        String fromUserName = "o6BSO5rDmYvhAubxBkk5gQONRcQk";//发送消息的微信用户id
        String toUserName = "gh_1046f29b0c76";//公众号ID
        String createTime = "1554883459";//时间戳
        String msgType = "text";//消息类型
        String content = "www";//回复内容
        String replyMsg = "<xml><ToUserName><![CDATA[%1$s]]></ToUserName>"
                            + "<FromUserName><![CDATA[%2$s]]></FromUserName>"
                            + "<CreateTime>%3$s</CreateTime>"
                            + "<MsgType><![CDATA[%4$s]]></MsgType>"
                            + "<Content><![CDATA[%5$s]]></Content></xml>";
        String handle = String.format(replyMsg, fromUserName, toUserName, createTime, msgType, content);
        System.out.println("回复公众平台加密前: " + handle);
        String mingwen1 = pc.encryptMsg(handle, timestamp, nonce);
        System.out.println("回复公众平台加密后: " + mingwen1);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        StringReader sr = new StringReader(mingwen1);
        InputSource is = new InputSource(sr);
        Document document = db.parse(is);

        Element root = document.getDocumentElement();
        NodeList nodelist1 = root.getElementsByTagName("Encrypt");

        String encrypt = nodelist1.item(0).getTextContent();

        String format = "<xml><ToUserName><![CDATA[%1$s]]></ToUserName>"
                              + "<Encrypt><![CDATA[%2$s]]></Encrypt></xml>";
        String fromXML = String.format(format, fromUserName, encrypt);
        System.out.println(fromXML);
    }
    
    
    
    
    
    
}
